package com.me.study.rabbitmq.config;

import org.springframework.amqp.core.Declarable;
import org.springframework.amqp.core.DeclarableCustomizer;
import org.springframework.amqp.core.Exchange;
import org.springframework.stereotype.Component;

import java.util.function.Function;

/**
 * @author light
 * @version 1.0
 * @description: 绑定备份交换机
 * @date 2021/10/19 15:15
 */
@Component
public class CustomizersBean implements DeclarableCustomizer {

    public String DIRECT = "DIRECT_EXCHANGE";
    public String BACKUP = "DIRECT_BACKUP_EXCHANGE";


    @Override
    public Declarable apply(Declarable declarable) {
        if (declarable instanceof Exchange) {
            Exchange exchange = (Exchange) declarable;
            if (exchange.getName().equals(DIRECT)) {
                // 绑定备份交换机
                exchange.addArgument("alternate-exchange", BACKUP);
            }
        }
        return declarable;
    }

    @Override
    public <V> Function<V, Declarable> compose(Function<? super V, ? extends Declarable> before) {
        return DeclarableCustomizer.super.compose(before);
    }

    @Override
    public <V> Function<Declarable, V> andThen(Function<? super Declarable, ? extends V> after) {
        return DeclarableCustomizer.super.andThen(after);
    }
}